/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cellstore.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author PPATOCHE
 */
public class Seguridad {

    private AuthenticationManager am;

    private Seguridad() {
        ApplicationContext context = new ClassPathXmlApplicationContext("contexto/Seguridad.xml");
        am = (AuthenticationManager) context.getBean("authenticationManager");
    }

    public void autenticar(String name, String password) {

        Authentication request = new UsernamePasswordAuthenticationToken(name, password);
        Authentication result = am.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
        String name1 = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(name1);
    }

    public static Seguridad getInstance() {
        return Seguridad.SeguridadHolder.INSTANCE;
    }

    private static class SeguridadHolder {

        private static final Seguridad INSTANCE = new Seguridad();
    }
}
